package com.letwal.cmcvnd.utils

import android.annotation.SuppressLint
import com.letwal.cmcvnd.data.*
import com.letwal.cmcvnd.extend.*


/** 用户登录状态管理工具 */
object UserStateUtil {

    //sp token
    var mToken: String

    //sp user id
    var mMobile: String

    //sp mobile
    var mUserId: String

    //sp reInstall
    var mReInstall: Boolean

    init {
        mToken = SpUtil.getToken()
        mUserId = SpUtil.getUserId()
        mMobile = SpUtil.getMobile()
        mReInstall = SpUtil.getReInstall()
    }

    /**
     * 更新登录信息
     *
     * @param revObjDoLogin 登录响应信息
     */
    fun updateLoginState(revObjDoLogin: RevObjDoLogin) {
        revObjDoLogin.mobile?.let {
            saveMobile(it)
        }

        revObjDoLogin.token?.let {
            saveToken(it)
        }

        revObjDoLogin.userId?.let {
            saveUserId(it)
        }
    }

    /**
     * 退出登录
     */
    fun logout() {
        //重置登录地区信息
        UsageCountryUtil.logout()

        //重置用户流程信息
        InfoProcessUtil.logout()

        //重置登录用户信息
        saveToken("")
        saveMobile("")
        saveUserId("")
    }

    /**
     * 判断是否已登录
     */
    fun isDidLogin(): Boolean {
        return (mToken.isNotBlank())
    }

    /** 保存 token */
    @SuppressLint("ApplySharedPref")
    fun saveToken(gToken: String) {
        mToken = gToken
        SpUtil.saveToken(gToken)
    }

    /** 保存 user id */
    @SuppressLint("ApplySharedPref")
    fun saveUserId(gUserId: String) {
        mUserId = gUserId
        SpUtil.saveUserId(gUserId)
    }

    /** 保存mobile */
    @SuppressLint("ApplySharedPref")
    fun saveMobile(gMobile: String) {
        mMobile = gMobile
        SpUtil.saveMobile(gMobile)
    }

    /** 保存reinstall */
    @SuppressLint("ApplySharedPref")
    fun saveReInstall(install: Boolean) {
        mReInstall = install
        SpUtil.saveReInstall(install)
    }


    /**
     * 获取用户姓名
     */
    fun getUserName(): String {
        return InfoProcessUtil.getUserName()
    }
}